package com.systop.student.controller;
import com.systop.common.pojo.Student;
import com.systop.common.util.ResponseResult;
import com.systop.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    //login
    @PostMapping(value = "/studentLogin")
    @ResponseBody
    public ResponseResult studentLogin(@RequestParam(value="sno") String sno, @RequestParam (value="password") String password) {
        Student student = new Student();
        student.setSno(sno);
        student.setPassword(password);
        Student student1 = studentService.SudentLogin(student);
        if (student1 == null){
            return  ResponseResult.build(500,"登陆失败");
        }else{
            return  ResponseResult.build(200,"登陆成功",student1);
        }
    }
    //查全部
    @PostMapping (value = "/studentSupList")
    @ResponseBody
    public ResponseResult studentSupList() {
        if (studentService.studentSupList() == null){
            return  ResponseResult.build(500,"登陆失败");
        }else{
            return  ResponseResult.build(200,"登陆成功",studentService.studentSupList());
        }
    }
    //根据tno查询
    @PostMapping (value = "/studentSupList1")
    @ResponseBody
    public ResponseResult studentSupList1(@RequestParam(value = "sno") String sno) {
            if (studentService.studentSupList1(sno)==null){
                return  ResponseResult.build(500,"根据tno查询失败");
            }else{
                return  ResponseResult.build(200,"根据tno查询成功",studentService.studentSupList1(sno));
            }
    }
    //添加
    @PostMapping (value = "/studentAdd")
    @ResponseBody
    public ResponseResult studentAdd(@RequestParam(value="sno") String sno, @RequestParam (value="password") String password,
                                     @RequestParam(value="name") String name, @RequestParam (value="college_name") String college_name,
                                     @RequestParam(value="grade") String grade, @RequestParam (value="class_name") String class_name,
                                     @RequestParam(value="remark") String remark) {
        Student student= new Student();
        student.setSno(sno);
        student.setPassword(password);
        student.setName(name);
        student.setCollege_name(college_name);
        student.setGrade(grade);
        student.setClass_name(class_name);
        int i = studentService.studentAdd(student);
        if (i==0){
            return  ResponseResult.build(500,"添加失败");
        }else{
            return  ResponseResult.build(200,"添加成功",i);
        }

    }
    //修改
    @PostMapping (value = "/updataStudent")
    @ResponseBody
    public ResponseResult updataStudent(@RequestParam(value="sno") String sno, @RequestParam (value="password") String password, @RequestParam(value="name") String name, @RequestParam (value="college_name") String college_name, @RequestParam(value="grade") String grade, @RequestParam (value="class_name") String class_name, @RequestParam(value="remark") String remark) {
        Student student = new Student();
        student.setSno(sno);
        student.setPassword(password);
        student.setName(name);
        student.setCollege_name(college_name);
        student.setGrade(grade);
        student.setClass_name(class_name);
        System.out.println(sno);
        System.out.println(password);
        int i = studentService.updataStudent(student);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }
            //删除
    @PostMapping (value = "/delStudent")
    @ResponseBody
    public ResponseResult delStudent(@RequestParam(value = "sno") String sno) {
        if (studentService.delStudent(sno)==0){
            return  ResponseResult.build(500,"删除失败");
        }else{
            return  ResponseResult.build(200,"删除成功",studentService.delStudent(sno));
        }
    }

}
